package com.asia5b.a5mall.middleware.logic.viewmodel.goods

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.base.ResultModel
import com.asia5b.a5mall.middleware.model.goods.Goods2CommenModel
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   数据控制器
 */
class GoodsCommentFragmentVM : SimpleViewModel() {

    private var page = 1
    private var sumPage = 1000


    val loadCommentLiveData = MutableLiveData<ResultModel<ArrayList<Goods2CommenModel>>>()

    /**
     * @Note   加载评论
     * @param  goodsId 商品ID
     * @param  state   评论等级
     * @param  page    查询分页
     */
    fun loadComment(goodsId: Int, state: String, refresh: Int) {
        if (refresh <= 1) {
            page = 1
            sumPage = 1000
        }
        if (page > sumPage) {
//            loadMvpView.dismissLoadingBar()
            loadingLiveData.value = 1
            return
        }
        val haveImage: Int? = if (state == "image") {
            1
        } else {
            null
        }
        val grade: String? = if (state.isEmpty()||state == "image") {
            null
        } else {
            state
        }
        val observable = memberApi.getComments(goodsId, grade, haveImage, page, 10)
        baseSubscribe(observable, SimpleNetParams(isShowLoading = refresh <= 1), Consumer {
            sumPage = it.sumPage
            page = it.page + 1//预备加载下一页
            loadCommentLiveData.value = it
        })
    }
}